<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\BuilderPlus\components\Column;
use addons\BuilderPlus\components\Edit;
use addons\BuilderPlus\components\Search;
use addons\BuilderPlus\facade\Table;
use think\Response;

class Carousel extends Base
{
    public function getOptions(): array
    {
        return [
            'pk' => 'id',
            'model' => \app\common\model\Carousel::class,
            'validate' => \app\admin\validate\Carousel::class,
            'append' => ['type_name']
        ];
    }

    public function index(): string
    {
        return Table::init($this)
            ->addColumns([
                ['id', 'ID', fn(Column $col) => $col->width(80)],
                ['title', '标题', null, fn(Edit $e) => $e->require()],
                ['type', '类型', fn(Column $col) => $col->templet('{{d.type_name}}'), function (Edit $e) {
                    $e->radio([1 => '图片', 2 => '视频'])->change([1 => 'images', 2 => 'video']);
                }],
                ['images', '轮播图', fn(Column $col) => $col->lazyImage()->width(100), fn(Edit $e) => $e->images()],
                ['video', '视频', fn(Column $col) => $col->lazyVideo()->width(100), fn(Edit $e) => $e->video()],
                ['status', '状态', fn(Column $col) => $col->switch('开启|关闭', '1|0')],
                ['sort', '排序', fn(Column $col) => $col->fastSort(), fn(Edit $e) => $e->number()],
                ['create_time', '添加时间', fn(Column $col) => $col->width(150)],
                ['update_time', '更新时间', fn(Column $col) => $col->width(150)]
            ])
            ->addTopButtons(['add', 'deleteAll'])
            ->addRightButtons(['edit', 'delete'])
            ->addSearch([
                ['title', '标题', fn(Search $search) => $search->condition('Like')]
            ])
            ->fetch();
    }

    public function save(array $param = [], string $method = ''): Response
    {
        $param = $this->request->post();
        if ($param['data']['type'] == 1) {
            $param['data']['video'] = '';
        } else {
            $param['data']['images'] = '';
        }

        return parent::save($param, $method);
    }
}